package com.ciwong.epaper.modules.reciteWords.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.reciteWords.adapter.FrgLearnedAdapter;
import com.ciwong.epaper.modules.reciteWords.bean.LearnedWordBean;
import com.ciwong.epaper.modules.reciteWords.bean.LearnedWordDayListBean;
import com.ciwong.epaper.modules.reciteWords.dao.WordDao;
import com.ciwong.epaper.modules.reciteWords.ui.ReciteWordLisActivity;
import com.ciwong.epaper.ui.BaseFragment;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.mobilelib.i.BaseCallBack;

import java.util.ArrayList;
import java.util.List;

public class TabLearnedFragment extends BaseFragment {
    private String TAG = "TabLearnedFragment";
    private List<LearnedWordDayListBean> list = new ArrayList<>();
    private LinearLayout ll_empty;
    private FrgLearnedAdapter frgLearnedAdapter;
    private RecyclerView rv_fragment_learned;
    private int count = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_word_list_learned, container, false);
        CWLog.i(TAG, "onCreateView");
        return initView(view);
    }

    private View initView(View view) {
        ll_empty = view.findViewById(R.id.ll_empty);
        rv_fragment_learned = view.findViewById(R.id.rv_fragment_learned);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        CWLog.i(TAG, "onActivityCreated");
        initEvent();
        initData();
    }


    private void initData() {
        getNetData();
    }

    private void initEvent() {
        frgLearnedAdapter = new FrgLearnedAdapter(getContext());
        rv_fragment_learned.setLayoutManager(new LinearLayoutManager(getContext()));
        rv_fragment_learned.setAdapter(frgLearnedAdapter);

    }

    private void handleView(List<LearnedWordDayListBean> list, int count) {
        //初始化时处理单词数量
        ReciteWordLisActivity activity = (ReciteWordLisActivity) getActivity();
        if (activity != null)
            activity.showWordCount(count);
        if (list != null && list.size() > 0) {
            CWLog.i(TAG, " initData 单词数量：" + list.size());
            ll_empty.setVisibility(View.GONE);
            rv_fragment_learned.setVisibility(View.VISIBLE);
        } else {
            ll_empty.setVisibility(View.VISIBLE);
            rv_fragment_learned.setVisibility(View.GONE);
        }
    }

    public int getWordNum() {
        return count;
    }


    public void getNetData() {
        int userId = (int) EApplication.getInstance().getUserInfoBase().getUserId();
        if (userId <= 0) return;
        showCricleProgress(null);
        WordDao.getInstance().getLearnedWords(userId, new BaseExtCallBack(getContext()) {
            @Override
            public void success(Object data) {
                hideCricleProgress();
                CWLog.d(TAG, " success getLearnedWords : " + data.toString());
                LearnedWordBean bean = (LearnedWordBean) data;
                List<LearnedWordDayListBean> learnedList = bean.getList();
                list.addAll(learnedList);
                count = bean.getCount();
                handleView(list, bean.getCount());
                frgLearnedAdapter.setData(list);
                rv_fragment_learned.scrollToPosition(0);
            }

            @Override
            public void failed(int errorCode, Object data) {
                hideCricleProgress();
                CWLog.d(TAG, "failed getLearnedWords : " + data.toString());
                super.failed(errorCode, data);
            }

            @Override
            public void failed(Object data) {
                failed(-1, data);
            }
        });

    }
}
